package com.ias.agdis_r.activity.task;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseActivity;
import com.ias.agdis_r.activity.MainActivity;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.FindLuggageDataManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponse;
import com.ias.agdis_r.terminalmanager.restful.findluggage.vo.FindLuggageInfoResponseVO;
import com.ias.agdis_r.terminalmanager.restful.findluggage.vo.LuggageMessageVO;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaowl on 2018/6/11.
 */
public class FindLuggageMessageActivity extends BaseActivity {

    ListView listView;
    BaseAdapter adapter;
    List<LuggageMessageVO> listMessage = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_luggage_message);
        initActionBar();

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar = getActionBar();
        actionBar.setCustomView(LayoutInflater.from(FindLuggageMessageActivity.this)
                .inflate(R.layout.actionbar_back_and_home, null));
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);
        TextView title = (TextView) findViewById(R.id.txt_title);
        title.setText(R.string.find_luggage_message_list);
        ImageView back = (ImageView) findViewById(R.id.action_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ImageView home = (ImageView) findViewById(R.id.action_home);
        home.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FindLuggageMessageActivity.this,
                        MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.list_view);
        View emptyView = findViewById(R.id.tv_empty);

        adapter = new FindLuggageMessageListAdapter(this, listMessage);
        listView.setAdapter(adapter);
        listView.setEmptyView(emptyView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LuggageMessageVO item = (LuggageMessageVO) parent.getItemAtPosition(position);
                String messageId = item.getMessageId();
                String tnb = getIntent().getStringExtra("tnb");

                Intent intent = new Intent(FindLuggageMessageActivity.this, FindLuggageActivity.class);
                intent.putExtra("tnb", tnb);
                intent.putExtra("messageId", messageId);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        String tnb = getIntent().getStringExtra("tnb");
        if (TextUtils.isEmpty(tnb)) {
            finish();
            return;
        }

        FindLuggageInfoResponseVO listFindLuggageInfo = FindLuggageDataManager.getInstance().getListFindLuggageInfo(tnb);
        if (listFindLuggageInfo == null) {
            EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_UPTO, getString(R.string.find_luggage_data_empty), false);
            finish();
            return;
        }

        listMessage.clear();
        List<LuggageMessageVO> luggageMessageVoList = listFindLuggageInfo.getLuggageMessageVoList();
        if (luggageMessageVoList != null && !luggageMessageVoList.isEmpty()) {
            listMessage.addAll(luggageMessageVoList);
        }

        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void operateResponseSucess(String serviceId, BaseResponse response) {

    }

    @Override
    protected void updateUI(String tag) {
        if (MessageModel.TLMF.equals(tag)) {
            initData();
        }
    }

    private static class FindLuggageMessageListAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private List<LuggageMessageVO> data;

        public FindLuggageMessageListAdapter(Context context, List<LuggageMessageVO> data) {
            this.inflater = LayoutInflater.from(context);
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public LuggageMessageVO getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.layout_find_luggage_message_item, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            LuggageMessageVO item = getItem(position);
            holder.textView.setText(item.getReceptionFgosMessage());
            holder.timeTv.setText(item.getReceptionTime());

            return convertView;
        }

        private static class ViewHolder {
            TextView textView;
            TextView timeTv;

            public ViewHolder(View view) {
                textView = (TextView) view.findViewById(R.id.tv_luggage_no);
                timeTv = (TextView) view.findViewById(R.id.tv_message_time);
            }
        }
    }

}
